<template>
	<view>
		<myModal ref="modal" width='600rpx' @close="diaClose" title="支付方式">
			<view class="payAccount">
				支付金额{{selectPrice}}元
			</view>
			<radio-group @change="radioChange">
				<view class="chooseBody">

					<view class="payWay" @click="choosePayWay('wechart')">
						<view class="descript">
							<image :src="wecharImg" class="paywayPic" mode="aspectFit"></image>
							<view class="context">
								微信支付
							</view>
						</view>
						<radio value="wechart" color="#ee5b25" :checked="payWay=='wechart'"></radio>
					</view>
					<view class="payWay" @click="choosePayWay('balance')">
						<view class="descript">
							<image :src="balanceImg" class="paywayPic" mode="aspectFit"></image>
							<view class="context">
								<view class="">
									余额支付
								</view>
								<view class="balance">
									账户余额{{currBalance}}元
								</view>
							</view>
						</view>
						<radio value="balance" :disabled="currBalance&&selectPrice&&currBalance<=selectPrice" color="#ee5b25" :checked="payWay=='balance'"></radio>

					</view>
				</view>
			</radio-group>
			<view class="modleFoot">
				<button type="primary" class="myBtn" size="mini" @click="commit">确定</button>
			</view>
		</myModal>
		<myModal ref="pwd" title="支付密码" width="500rpx">
			<view class="inputContain">
				<input type="password" @input="pwdChange" :maxlength="8" :value="payPwd" />
			</view>
			<view class="inputContain">
				<view class="paybtn" @click="doPay">
					支付
				</view>
			</view>
		</myModal>
	</view>


</template>

<script>
	import myModal from "@/components/myModal/myModal"
	import {
		userInfo
	} from "@/common/loadUserInfo.js"
	export default {
		components: {
			myModal
		},
		props: {
			selectPrice: Number,
			sourceId: Number
		},
		data() {
			return {
				payPwd: '',
				payWay: 'wechart',
				currBalance: 0,
				hasPayPwd: false,
				joinSuccess: false,
				wecharImg: require('../static/images/payment-wechat.png'),
				balanceImg: require('../static/images/payment-balance.png')
			};
		},

		methods: {
			pwdChange(e) {
				this.payPwd = e.detail.value
			},
			beginPay() {
				this.joinSuccess = false
				this.$refs.modal.open()
				let that = this
				userInfo({
					success: (res) => {
						console.log(res)
						that.hasPayPwd = res.data.userinfo.payPwd ? true : false
						that.currBalance = res.data.userinfo.userMoney
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			radioChange(e) {
				this.payWay = e.detail.value
			},
			choosePayWay(payway) {
				if (payway == 'balance' && this.currBalance <= this.selectPrice) {
					return
				}
				this.payWay = payway
			},
			doPay() {
				this.$refs.pwd.close()
				let payType = this.payWay == 'balance' ? 1 : 2
				let that = this
				let data = {
					id: that.sourceId,
					payType,
					payMoney: that.selectPrice
				}
				if (payType == 1) {
					data.payPwd = that.payPwd
				} else if (payType == 2) {
					that.$api.msg('app pay')
				}



				that.$Request.request('/api/pintuan/buy', 'POST', data).then(res => {
					console.log(res)
					if (res.status == 1) {
						if (payType == 1) {
							that.joinSuccess = true
							that.$refs.modal.close()
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/lottery/lottery?pinId=' + res.data.orderNo
								})
							}, 800)
						} else if (payType == 2) {
							that.$api.msg('app pay')
						}


					} else {
						if (payType == 1) {
							that.payPwd = ''
						}
						if (res.msg == '您未设置支付密码，请先设置密码') {
							uni.navigateTo({
								url: '/subPackage/payPwd/payPwd?type=reset'
							})
						}
					}

				}).catch(err => {
					console.log(err)

				})
			},
			commit() {
				// uni.navigateTo({
				// 	url:'/subPackage/payPwd/payPwd?type=enter'
				// })
				// this.joinSuccess=true
				// this.$refs.modal.close()
				let payType = this.payWay == 'balance' ? 1 : 2
				if (payType == 1) {
					if (this.hasPayPwd) {
						this.$refs.pwd.open()
					} else {
						this.$api.msg('您未设置支付密码，请先设置密码')
						setTimeout(() => {
							uni.navigateTo({
								url: '/subPackage/payPwd/payPwd?type=reset'
							})
						}, 800)
					}

				} else {
					// this.$api.msg('微信支付')
					this.doPay()
				}
			},
			diaClose() {
				this.$emit('commitJoin', this.joinSuccess)
			}
		}
	}
</script>

<style lang="scss">
	.payAccount {
		text-align: center;
		padding: 50rpx;
		font-weight: bold;
	}

	.inputContain {
		height: 110rpx;
		@extend %flexAlCenter;

		input {
			background-color: #F8F8F8;
			width: 80%;
			height: 60rpx;
		}

		.paybtn {
			background-color: $themColor;
			border-radius: 8rpx;
			padding: 0 26rpx;
			@include textCenter(55rpx);
			color: #fff;
			letter-spacing: 0.2em;
		}
	}

	.chooseBody {
		padding: 0 30rpx;
	}

	.payWay {
		@extend %flexBetw;

		padding: 20rpx 0;
		border-bottom: 2rpx solid #ccc;
	}

	.payAccount {
		text-align: center;
		padding: 50rpx;
		font-weight: bold;
	}

	.descript {
		@extend %flexAlCenter;

		.context {
			font-size: 28rpx;
			@extend %flexBetw;
			flex-direction: column;
			padding-left: 1em;
			height: 100rpx;
			align-items: flex-start;

			.balance {
				font-size: 22rpx;
				color: #ccc;
			}
		}

		.paywayPic {
			width: 100rpx;
			height: 100rpx;
			z-index: 11;
		}

	}

	.modleFoot {
		width: 100%;
		height: 110rpx;
		@extend %flexAlCenter;
	}

	.myBtn {
		background-color: $themColor !important;
		color: #fff !important;
		height: 60rpx !important;
		line-height: 60rpx !important;
	}
</style>
